package com.cssy.yd.basic.web;


import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cssy.authority.entity.ScDepartmentDO;
import com.cssy.authority.service.IScDepartmentService;
import com.cssy.base.web.BaseController;
import com.cssy.base.web.BodyMsg;
import com.cssy.yd.basic.entity.BasicAreaDO;
import com.cssy.yd.basic.entity.BasicStorageinfoDO;
import com.cssy.yd.basic.entity.BasicStorageinfoTypeDO;
import com.cssy.yd.basic.service.IBasicAreaService;
import com.cssy.yd.basic.service.IBasicStorageInfoTypeService;
import com.cssy.yd.basic.service.IBasicStorageService;
import com.plat.helper.JqGridHandler;
import com.plat.helper.SearchCondition;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.GlobalResLoader;
import com.plat.util.OperationFlag;
import com.plat.util.SessionUtil;
import com.plat.util.json.OutJsonGrid;
import com.yuendong.sy.po.code.SyCode;
import com.yuendong.sy.service.code.SyCodeType;
import com.yuendong.sy.service.code.impl.SyCodeService;
@Controller
@RequestMapping("/yd/basic/wh")
public class BasicStorageController extends BaseController<BasicStorageinfoDO>{
	private static final String MANAGER = "/yd/basic/wh/WHManager";//列表页面
	private static final String EIDT = "/yd/basic/wh/WHOperation";//添加、修改、显示页面
	private static final String MAIN = "/yd/basic/wh/WHMain";//首页流程图
	@Resource(name="syCodeService")
	private SyCodeService syCodeService;
	
	@Resource(name = "basicwhService")
	private IBasicStorageService basicwhService;
	
	@Resource(name = "basicwhTypeService")
	private IBasicStorageInfoTypeService basicwhTypeService;

	 @Resource(name = "scDepartmentService")
	 private IScDepartmentService scDepartmentService;
	 
	 @Resource(name="basicAreaService")
	private IBasicAreaService basicAreaService;
	 
	public String manager(HttpServletRequest request, Model model)
			throws Exception {
		return MANAGER;
	}
	
	
	@Override
	public void list(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JqGridHandler hander=new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		if(scs.getOrderBy()==null||"".equals(scs.getOrderBy()))
			scs.setOrderBy("dctime desc");
		SearchResult result =	basicwhService.findAllByExample(scs);
//		List<BasicStorageinfoDO> list = result.getResults();
//		for (BasicStorageinfoDO basicStorageinfoDO : list) {
//		}
		
		OutJsonGrid out=new OutJsonGrid(result);
		out.printJSon(response);
		
	}

	@Override
	public String operationForm(HttpServletRequest request,@PathVariable String id,
			@PathVariable String flag, Model model) throws Exception {
		SearchConditions scs = new SearchConditions();
		
		JqGridHandler hander=new JqGridHandler(request);
		hander.getWheres(scs);	
		SearchResult s =basicwhTypeService.findListByExample(scs);
		List<BasicStorageinfoTypeDO> type = s.getResults();
		
//		JqGridHandler hander2 = new JqGridHandler(request);
		SearchConditions scs2 = new SearchConditions();
		hander.getWheres(scs2);
		
//		JqGridHandler hander3=new JqGridHandler(request);
		SearchConditions scs3 = new SearchConditions();
		hander.getWheres(scs3);
		scs3.addCondition("use_system", "ERP",SearchCondition.OP_EQUAL);
		List<BasicAreaDO> area =	basicAreaService.findListByExample(scs3).getResults();
		
		
		SearchResult result = scDepartmentService.findListByExample(scs2);
		List<ScDepartmentDO> dept = result.getResults();
		if(OperationFlag.ADD.equals(flag)){//我新增加时操作
			model.addAttribute("flag","add");
		}else{
			BasicStorageinfoDO wh=	basicwhService.findListById(id);
			
			model.addAttribute("wh", wh);
			model.addAttribute("flag","edit");
		}
		model.addAttribute("type", type);
		model.addAttribute("dept", dept);
		model.addAttribute("area", area);
		return EIDT;
	}
	
	@RequestMapping("/findWHpsizeByWHcode")
	@ResponseBody
	public String findWHpsizeByWHcode(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
//		String whcode = request.getParameter("whcode");
		return null;
	}
	
	
	@Override
	public Map<String, Object> save(HttpServletRequest request,
			BasicStorageinfoDO curAP, BindingResult result, Model model)
	{
		BodyMsg bodyMap=BodyMsg.getInstance();
		
		try {
			if(curAP.getId()!=null && !"".equals(curAP.getId())){//修改
				basicwhService.update(curAP, SessionUtil.getUser(request));
				bodyMap.isSuccess();
				bodyMap.configWebInfor(GlobalResLoader.getOpsuccess());
				bodyMap.configWebId(curAP.getId());
			}else{//新增
				if(curAP.getCode()!=null&&!"".equals(curAP.getCode())){
					BasicStorageinfoDO bdo = new BasicStorageinfoDO();
					bdo.setCode(curAP.getCode());
					List<BasicStorageinfoDO> codeInfo = basicwhService.findListByExample(bdo);
					curAP.setRemoved(Short.parseShort("0"));
					curAP.setIsopen(Short.parseShort("1"));
					boolean flag = false; 
					if(codeInfo!=null && !codeInfo.isEmpty()){
						for (BasicStorageinfoDO db : codeInfo ){
							if(db.getCode().equals(bdo.getCode())){
								flag = true;
							}
						}
					}
					if (flag){
							bodyMap.isFail();
							bodyMap.configWebInfor("对不起您输入的编码已存在");
							return bodyMap.modelMap;
					}
				}else{
					curAP.setCode(syCodeService.getCode("WH","WH",SyCode.DATE_YYMM,5));
				}
				BasicStorageinfoDO cdo=basicwhService.save(curAP, SessionUtil.getUser(request));
				bodyMap.isSuccess();
				bodyMap.configWebInfor(GlobalResLoader.getOpsuccess());
				bodyMap.configWebId(cdo.getId());
			}
		} catch (Exception e) {			
			e.printStackTrace();
			bodyMap.isFail();
			bodyMap.configWebInfor(GlobalResLoader.getOpfailed()+e.getMessage());
		}
		return bodyMap.modelMap;
		
	}
		

	@Override
	public Map<String, Object> delete(HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {
		BodyMsg bodyMap=BodyMsg.getInstance();
		try{
			String ids = request.getParameter("ids");
			String[] fields=ids.split(",");
			
			for(String field : fields){
				basicwhService.delete(field, SessionUtil.getUser(request));
			}
		
			bodyMap.isSuccess(GlobalResLoader.getOpsuccess());
		}catch(Exception e){
			e.printStackTrace();
			bodyMap.isFail(GlobalResLoader.getOpfailed()+e.getMessage());
		}
		return bodyMap.modelMap;
	}
	
	/**
	 * 模块首页
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @author huang.xuming [2013-9-3]
	 */
	@RequestMapping("/main")
	public String mainpage(HttpServletRequest request,
			HttpServletResponse response, Model model){
		return MAIN;
	}
}
